package com.mykt.service;

import com.mykt.baseResult.RespondsResult;
import com.mykt.constant.LoginConstants;
import com.mykt.constant.SecurityConstants;
import com.mykt.domain.SystemLoginLog;
import com.mykt.exception.MyServiceException;
import com.mykt.system.RemoteUserService;
import com.mykt.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 系统登录日志记录
 */
@Component
public class LoginLogService {

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 添加系统登录日志
     * @param userName  用户名
     * @param status    状态
     * @param message   消息内容
     */
    public void recordLoginLog(String userName,String status,String message){
        SystemLoginLog systemLoginLog=new SystemLoginLog();
        systemLoginLog.setUserName(userName);
        systemLoginLog.setMsg(message);
        systemLoginLog.setIpAddr("127.0.0.1");

       if(StringUtils.isAnyBlank(status, LoginConstants.LOGIN_SUCCESS,LoginConstants.REGISTER)){
            systemLoginLog.setStatus(LoginConstants.SUCCESS_STATUS);
        }else if(LoginConstants.LOGIN_FAIL.equals(status)){
            systemLoginLog.setStatus(LoginConstants.ERROR_STATUS);

        }
        RespondsResult respondsResult = remoteUserService.add(systemLoginLog, SecurityConstants.INNER);
        if(respondsResult.getCode() != 1){
            throw new MyServiceException(respondsResult.getMsg());
        }


    }

}
